function formatDate (strTime) {
    if(strTime == null)
        return "";
    var date = new Date(strTime);
    return date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate();
}
function formatTime (strTime) {
    if(strTime == null)
        return "";
    var date = new Date(strTime);
    return date.getFullYear()+"-"+(date.getMonth()+1)+"-"+date.getDate()+" "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
}
function getAllCityForPackageInquire(parentId,obj){
    $.getJSON("../address_query.json",{parentId : parentId},function(data){
        if(data.success && data.address != null){
            $.each(data.address,function(index,item){
                obj.append("<option  value="+item.name+">"+item.name+"</option>");
            });
        }
    });
}
function getBranchStationForPackageInquire(transferStation) {
    $.getJSON("../stationManage_querySubStationByParentId.json",{"id":parseInt(transferStation)},function (data) {
        if(data.success){
            $(".packace_arrive3").append("<option value='-3'>所有支站点</option>");
            $.each(data.users,function (n,value) {
                $(".packace_arrive3").append("<option value='"+value.id+"'>"+value.stationName+"</option>");
            });
        }
    });
}
function getTransferStationForPackageInquire(city,area) {
    if(city=="")return;
    $.getJSON("../stationManage_findStationOfCity.json",{"city":city,"area":area},function (data) {
        if(data.success){
            $(".packace_arrive2").append("<option value='-2'>所有中转站</option>");
            $.each(data.stations,function (n,value) {
                $(".packace_arrive2").append("<option value='"+value.id+"'>"+value.stationName+"</option>");
            });
        }
    });
}
//根据包裹的id查询包裹下的订单列表
function getOrdersOFPackage(id){
	$.getJSON("../packageManage_queryOrdersOfPackage.json",{id:id},function (data) {
		$(".orders_data").remove();
		 $(".layer").css("display","block");
		if(data.success){
			$.each(data.orders,function (i,item) {
				$("#orderdsOfpakage").append("<tr  class='orders_data'>" +
						"<td>"+item.orderNo+"</td><td>" 
						+item.toName+"</td><td>" 
						+item.cellphone+"</td><td>" 
						+item.city+item.area+item.street+item.address+"</td><td>" 
						+item.user.stationName+"</td><td> "
						+(item.status==1?'出发站已揽收':item.status==2?'中转站已揽收':item.status==3?'中转站已发出':item.status==4?'到达目的地站点':item.status==5?'派件完毕':item.status==6?'12中转站驳回':item.status==14?'收件站点驳回':item.status==15?'派送失败':'')
						+"</tr>");
			})
		}
    });
}

function addCarNum(id,obj){
	var packageId=id;
	console.log(obj);
	var carNumber=$(obj).parent().children()[0].value;
	if(carNumber==undefined){
		window.wxc.xcConfirm("请先输入车牌号再进行保存！", window.wxc.xcConfirm.typeEnum.success);
	}else{
		$.getJSON("../packageManage_addCarNumberToPackage.json",{packageId:packageId,carNumber:carNumber},function (data) {
			if(data.success){
				window.wxc.xcConfirm(data.msg, window.wxc.xcConfirm.typeEnum.success);
			}
			else{
				window.wxc.xcConfirm(data.msg, window.wxc.xcConfirm.typeEnum.error);
			}
			
		})
	}
}
function queryPackage(){
 	if($("#txtEndDate").val()==""){
		var date = "";
	}
	else{
		var date = formatDate($("#txtEndDate").val());
	}
	
    var startCity = $(".packace_resource").val();
    var arriveCity = $(".packace_arrive1").val();
    var arriveTransferStation =  $(".packace_arrive2").val();
    var arriveSubStation =  $(".packace_arrive3").val();
	$.getJSON("../packageManage_queryPackage.json",{createTime:date,packageCity:startCity,destMid:arriveTransferStation,destSub:arriveSubStation,destCity:arriveCity},function (data) {
		$(".package_data").remove();
		if(data.success==true){
			$.each(data.packages,function (i,item) {
				$.getJSON("../stationManage_queryStationNameById.json",{id:item.destStationId},function (data) {
			        if(data.success){
			        	$("#package_box").append("<tr  class='package_data'><td>" +
			        			"<label><input  type=\"text\" name=\"carNumber\" value=\""+(item.carNumber==null?'':item.carNumber)+"\"/><button onclick=\"addCarNum("+item.id+",this)\" class=\"addCarNum\" value=\""+item.id+"\">保存</button></label>"+"</td>"
	    						+"<td  class='check_package'>"+item.packageNo+"</td><td>" 
	    						+data.stationName+"</td><td>" 
	    						+(item.packageUserName==null?'':item.packageUserName)+"</td><td>" 
	    						+formatDate(item.createTime)+"</td><td>" 
	    						+item.packageCity+"</td><td>"
	    						+"<button onclick=\"getOrdersOFPackage("+item.id+")\">查看</button>"
	    						+"</td></tr>");
			        
			        	
			        };
				})
				
			});
		}
	});
}


$(function () {
	
    $(".stop").click(function () {
        $(".layer").css("display","none");
    })
	getAllCityForPackageInquire(0,$(".packace_arrive1"));
    getAllCityForPackageInquire(0,$(".packace_resource"));
    $(".packace_resource").change(function () {
        var city = $(".packace_resource option:selected").text();
        if(city=="所有城市"){
            $(".packace_arrive1").val("").attr("disabled","disabled");
            $(".packace_arrive2").val("").attr("disabled","disabled");
            $(".packace_arrive3").val("").attr("disabled","disabled");
        }else {
            $(".packace_arrive1").removeAttr("disabled");
            $(".packace_arrive2").removeAttr("disabled");
            $(".packace_arrive3").removeAttr("disabled");
        }
    });
    $(".packace_arrive1").change(function () {
        var city = $(".packace_arrive1 option:selected").val();
        var area;
        $(".packace_arrive2 option:gt(0)").remove();
        getTransferStationForPackageInquire(city,area);
    });
    $(".packace_arrive2").change(function () {
        if($(".packace_arrive2 option:selected").text()=="所有中转站"){
            $(".packace_arrive3").val("").attr("disabled","disabled");
        }else {
            $(".packace_arrive3").removeAttr("disabled");
            $(".packace_arrive3 option:gt(0)").remove();
            transferStation = $(".packace_arrive2 option:selected").val();
            getBranchStationForPackageInquire(transferStation);
        }
    });
    $(".clearPackage").click(function () {
        $("#txtEndDate").val("");
        $(".packace_resource").val();
        $(".packace_arrive1").val();
        $(".packace_arrive2").val();
        $(".packace_arrive3").val();
    });
});
	